<template>
  <div style="margin-left: 10px">
    <div style="margin-bottom: 10px; padding-top: 10px">
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="$emit('showMain')">返回</el-button>
      <span style="margin-left: 10px">评估日期</span>
      <el-date-picker
        v-model="value1"
        end-placeholder="结束日期"
        range-separator="至"
        start-placeholder="开始日期"
        style="margin-left: 10px"
        type="daterange"
        value-format="yyyy-MM-dd"
      />
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" width="50" />
      <el-table-column label="目标" prop="title" />
      <el-table-column label="状态" prop="status_name">
        <template slot-scope="scoped">
          <el-radio v-model="scoped.row.ability" label="Y">会</el-radio>
          <el-radio v-model="scoped.row.ability" label="N">不会</el-radio>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-button
        style="margin-top: 10px; margin-bottom: 10px"
        type="primary"
        @click="add"
      >
        新增描述
      </el-button>
      <el-table :data="tableData1" style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column label="问题名称" prop="title" />
        <el-table-column label="内容" prop="content" />
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button type="danger" @click="delteData(scoped)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" label-width="80px" :model="form">
        <el-form-item label="问题名称">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addsubmit(form)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  // customerCaseDetail
  // editPrepare
  import { editPrepare, feedBackAdd } from '@/api/edu'

  //
  export default {
    props: {
      datas: {
        type: Object,
        required: true,
      },
      dataupl: {
        type: Object,
        required: true,
      },
    },
    data() {
      return {
        tableData: [],
        tableData1: [],
        value1: '',
        dialogVisible: false,
        form: {
          title: '',
          content: '',
        },
      }
    },
    mounted() {
      if (this.dataupl.id) {
        const ability_json = JSON.parse(this.dataupl.ability_json)
        console.log(ability_json)
        this.tableData = ability_json.objectives
        this.tableData1 = ability_json.describe
        this.value1 = [this.dataupl.start_date, this.dataupl.end_date]
      } else {
        this.init()
      }
    },
    methods: {
      init() {
        const data = {
          id: this.datas.id,
        }
        editPrepare(data).then((e) => {
          this.tableData = []
          const objectives = JSON.parse(e.data.objectives)
          objectives.forEach((k) => {
            k.objectives.forEach((i) => {
              i.ability = ''
              this.tableData.push(i)
            })
          })
        })
      },

      FamilyChange(e) {
        this.FamilyOptions.forEach((res) => {
          if (res.group_id == e) {
            this.userOptions = res.child
          }
        })
      },
      goBack() {
        // 返回
        this.$emit('showMain')
      },
      add() {
        this.dialogVisible = true
        this.form = {
          title: '',
          content: '',
        }
      },
      addsubmit(e) {
        if (e.title && e.content) {
          this.tableData1.push(e)
          this.dialogVisible = false
        } else {
          this.$message.error('填写内容')
        }
      },
      delteData(e) {
        console.log(e.$index)
        this.tableData1.splice(e.$index, 1)
      },
      onSubmit() {
        const a = {
          objectives: this.tableData,
          describe: this.tableData1,
        }
        const data = {
          prepare_id: this.datas.id,
          start_date: this.value1[0],
          end_date: this.value1[1],
          ability_json: JSON.stringify(a),
        }
        if (this.dataupl.id) {
          data.id = this.dataupl.id
        }
        feedBackAdd(data).then(() => {
          this.$emit('showMain')
        })
      },
    },
  }
</script>

<style></style>
